<?php
namespace app\backstage\logic;
use think\Db;
class RechargeRepeated
{
    /*列表数据获取*/
    public function getList($params)
    {
        $field = 'report_date,new_user_num,new_jiazhuo_num,rechargers_num,accumulated_recharge_num,accumulated_recharge_money,cost';
        $field .= ',profit,jiazhuo_cost,order_cost,accumulated_rechargers_num,t2,t3,t4';

        $groupBy = '';
        $type = 3;
        $where = [];
        $uid = request()->_user->id;

        if(isset($params['channel_id']) && $params['channel_id'] > 0)
        {
            $where[] = ['channel_id','=',$params['channel_id']];
            $type = 2;
        }else{
            if(request()->_role->alias == 'agent')
            {
                $type = 1;
                $where[] = ['agent_id','=',$uid];
            }
            if(request()->_role->alias == 'channel')
            {
                $where[] = ['channel_id','=',$uid];
                $type = 2;
            }
        }

        if (isset($params['start_date']) && strlen($params['start_date']) > 1) {
            $where[] = ['report_date', '>=', $params['start_date']];
        }

        if (isset($params['end_date']) && strlen($params['end_date']) > 1) {
            $where[] = ['report_date', '<=', $params['end_date']];
        }

        if (isset($params['field'])) {
            $orderBy = [$params['field'] => $params['order']];
        } else {
            $orderBy = ['report_date' => 'DESC'];
        }

        $CostReport = \think\facade\App::model('RechargeRepeated');
        $list = $CostReport->getList($type, $where, $field, $params['page'], $params['limit'], $orderBy, $groupBy);

        if ($list) {

            $listAll = $CostReport->getAll($type,$where, $field, ['report_date' => 'DESC'], $groupBy);



            foreach ($list as $k => $v) {

                if($v['profit'] < 0)
                {
                    $list[$k]['profit']     = '-'.getQuotient(abs($v['profit']),100);
                }else{
                    $list[$k]['profit']     = getQuotient($v['profit'],100);
                }
                $list[$k]['accumulated_recharge_money'] = getQuotient($v['accumulated_recharge_money'],100);
                $list[$k]['cost']           = getQuotient($v['cost'],100);
                $list[$k]['jiazhuo_cost']   = getQuotient($v['jiazhuo_cost'],100);
                $list[$k]['order_cost']     = getQuotient($v['order_cost'],100);
                $list[$k]['t1']            = getQuotient($v['rechargers_num'],$v['accumulated_recharge_num'],2,'m',100).'%';
                $list[$k]['t2']            = getQuotient($v['t2'],$v['accumulated_recharge_num'],2,'m',100).'%';
                $list[$k]['t3']            = getQuotient($v['t3'],$v['accumulated_recharge_num'],2,'m',100).'%';
                $list[$k]['t4']            = getQuotient($v['t4'],$v['accumulated_recharge_num'],2,'m',100).'%';

                $list[$k]['total_recharges_ratio'] = getQuotient($v['accumulated_rechargers_num'],$v['accumulated_recharge_num'],2,'m',100).'%';
            }

        }

        $result['count'] = $CostReport->getTotalNumber($type, $where, $groupBy);
        $result['list']  = $list;

        $total_cost = isset($listAll) ? array_sum(array_column($listAll,'cost')) : 0;
        $total_recharge = isset($listAll) ? array_sum(array_column($listAll,'accumulated_recharge_money')) : 0;

        $result['summary']['total_cost'] = getQuotient($total_cost,100);
        $result['summary']['total_recharge'] = getQuotient($total_recharge,100);
        $result['summary']['total_recovery_rate'] = getQuotient($total_recharge,$total_cost,2,'m',100).'%';

        return $result;
    }


}